﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using zhangmenren.IService;
using zhangmenren.Models;

namespace zhangmenren.Areas.Admin.Controllers
{
	[Area(areaName: "Admin")]
	public class CommentController : ABaseController
	{
		private ICommentServic commentServic;
		public CommentController(ICommentServic _commentServic)
		{
			commentServic = _commentServic; 
		}

		/// <summary>
		/// 评论列表页面
		/// </summary>
		/// <returns></returns>
		public IActionResult Index()
        {
            return View();
        }

		/// <summary>
		/// 获取评论列表
		/// </summary>
		/// <param name="page"></param>
		/// <returns></returns>
		public async Task<string> GetComment(int page = 1)
		{
			int total = 0;
			IEnumerable<CommentEntity> dataList = null;
			dataList = await commentServic.Select();
			if (dataList.Any())
			{
				total = dataList.Count();
			}
			return JsonConvert.SerializeObject(new { code = "0", msg = "查询成功", data = dataList, count = total });

		}


		/// <summary>
		/// 根据标识删除评论
		/// </summary>
		/// <param name="id"></param>
		/// <returns></returns>
		[HttpPost]
		public async Task<JsonResult> Del(int id)
		{
			if (id == 0)
			{
				return Json(new { state = "fail", msg = "删除错误,请刷新后重试." });
			}
			var result = await commentServic.Del(id);
			return Json(new { state = "ok", msg = "删除成功" });
		}



        [HttpPost]
        public async Task<JsonResult> Up(int id,string huifu)
        {
            if (id == 0)
            {
                return Json(new { state = "fail", msg = "回复错误,请刷新后重试." });
            }
            var result = await commentServic.Up(id,huifu);
            return Json(new { state = "ok", msg = "回复成功" });
        }

    }
}